package com.apes.enquiry.brand.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.enquiry.brand.repository.BrandDao;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.ApesBean;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.Map;

/**
 * 类描述：
 *
 * @ClassName SpBrand
 * @Description 描述
 * @Author wushilong
 * @Date 2022/11/3 13:47
 * @Version 1.0
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SpBrand.CONST_TABLE_NAME)
public class SpBrand implements ApesBean {
    public static final String CONST_TABLE_NAME = "sp_mas_ppxx";

    @Id
    @Column(name = "id", length = 8)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "品牌列表", padding = 7)
    @Comment("品牌编码")
    private String id;

    @NotNull
    @Column(name = "PPMC")
    @Comment("品牌名称")
    private String name;

    @Column(name = "YWMC")
    @Comment("英文名称")
    private String enName;

    @Column(name = "PPLXID", length = 8)
    @Comment("品牌类型编码")
    private String brandTypeId;

    @Column(name = "PPLXMC", length = 64)
    @Comment("品牌类型")
    private String brandTypeName;

    @Column(name = "PYM", length = 64)
    @Comment("拼音码")
    private String pinYinMa;

    @Column(name = "PZID", length = 8)
    @Comment("品质编码")
    private String qualityId;

    @Column(name = "PZMC", length = 255)
    @Comment("品质名称")
    private String qualityName;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "quality", referencedColumnName = "id")
    @Comment("品质")
    private Enumeration quality;

    @Column(name = "TS01", nullable = false, length = 36)
    private String time; //时间戳

    @NotNull
    @Column(name = "LRR", length = 64)
    @Comment("录入人")
    private String creatorName;

    @Column(name = "XFPZID", length = 64)
    @Comment("细分品质Id")
    private String subdivideBrandId;

    @Column(name = "XFPZMC", length = 64)
    @Comment("细分品质名称")
    private String subdivideBrandName;


    @NotNull
    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "LRRQ_SJ")
    @Comment("录入日期")
    private Date entryDate; //录入日期

    @NotNull
    @Column(name = "YXBJ", columnDefinition = "number(2) default 1")
    @Comment("有效标记")
    private boolean valid = true;//有效标记

    @Transient
    private Map<String,String> subQuality;

    public Map<String, String> getSubQuality() {
        this.subQuality = SpringManager.getBean(BrandDao.class).findSubQuality(subdivideBrandId);
        return this.subQuality;
    }

    public void setSubQuality(Map<String, String> subQuality) {
        this.subQuality = subQuality;
    }
}
